
import { weekSales, monthSales, yearSales, weekSpread, monthSpread, yearSpread } from '@/api/sales'
import 'moment/locale/zh-cn'

const moment = require('moment')
moment.locale('zh-cn')
export default {
  namespaced: true,
  state () {
    return {
      week: {
        start: moment().startOf('week').format('YYYY-MM-DD'),
        end: moment().subtract(0, 'days').format('YYYY-MM-DD')
      },
      month: {
        start: moment().startOf('month').format('YYYY-MM-DD'),
        end: moment().subtract(0, 'days').format('YYYY-MM-DD')
      },
      year: {
        start: moment().startOf('year').format('YYYY-MM-DD'),
        end: moment().subtract(0, 'days').format('YYYY-MM-DD')
      },
      Sales: {},
      Spread: {}
    }
  },

  mutations: {
    setWeekSales (state, payload) {
      const list = []
      const datalist = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
      payload.xAxis.forEach(item => {
        list.push(datalist[new Date(item).getDay()])
      })
      state.Sales = { series: payload.series, xAxis: list }
    },
    setMonthSales (state, payload) {
      state.Sales = payload
    },
    setYearSales (state, payload) {
      state.Sales = payload
    },
    setWeekSpread (state, payload) {
      state.Spread = payload
    },
    setMonthSpread (state, payload) {
      state.Spread = payload
    },
    setYearSpread (state, payload) {
      state.Spread = payload
    }
  },
  actions: {
    async getWeek (context) {
      const res = await weekSales(context.state.week)
      const res1 = await weekSpread(context.state.week)
      context.commit('setWeekSales', res)
      context.commit('setWeekSpread', res1)
    },
    async getMonth (context) {
      const res = await monthSales(context.state.month)
      const res1 = await monthSpread(context.state.month)
      context.commit('setMonthSales', res)
      context.commit('setMonthSpread', res1)
    },
    async getYear (context) {
      const res = await yearSales(context.state.year)
      const res1 = await yearSpread(context.state.year)
      console.log(res1)
      console.log(res)
      context.commit('setYearSales', res)
      context.commit('setYearSpread', res1)
    }
  }
}
